<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button v-if="isAuth('kernel:user:form')" type="success" icon="Plus" @click="router.push({ name: 'kernel_user_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-select
            v-model="dataForm.organization_id"
            :placeholder="i18n('common.please_select') + i18n('organization.title')"
            clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in organizationList" :label="v.title" :key="k" :value="v.id">
            </el-option>
          </el-select>
        </div>
        <div class="input">
          <el-input v-model="dataForm.nickname" :placeholder="i18n('common.please_input') + i18n('user.nickname')" clearable></el-input>
        </div>
        <div class="input">
          <el-input v-model="dataForm.mobile" :placeholder="i18n('common.please_input') + i18n('user.mobile')" clearable></el-input>
        </div>
        <div class="date">
          <el-date-picker v-model="dataForm.last_login_time" type="daterange" :range-separator="i18n('common.to')" :start-placeholder="i18n('common.start_time')" :end-placeholder="i18n('common.end_time')" clearable></el-date-picker>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70">
          </el-table-column>

          <el-table-column :label="i18n('organization.title')" width="140">
            <template v-slot="scope">
              <span v-if="scope.row.organization">
                {{ scope.row.organization.title }}
              </span>
              <span v-else>
                平台用户
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('user.user_role_name')" width="100">
            <template #default="scope">
              <span v-if="scope.row.role">
                {{ scope.row.role.title }}
              </span>
            </template>
          </el-table-column>

          <el-table-column prop="nickname" :label="i18n('user.nickname')">
            <template #default="scope">
              <dl class="table_dl">
                <dt>
                  <el-avatar :size="45" :src="scope.row.avatar">
                    <img src="@/assets/images/default/circle.png" />
                  </el-avatar>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('user.nickname') }}： {{ scope.row.nickname }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('user.create_time') }}： {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column prop="username" :label="i18n('user.username')" width="120">
          </el-table-column>

          <el-table-column prop="sex" :label="i18n('user.sex')" width="120">
            <template v-slot="scope">
              {{ scope.row.sex.text }}
            </template>
          </el-table-column>

          <el-table-column :label="i18n('user.position')" width="120">
            <template v-slot="scope">
              <span v-if="scope.row.detail">
                {{ scope.row.detail.position }}
              </span>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('user.is_bind')" width="100">
            <template v-slot="scope">
              {{ scope.row.is_bind.text }}
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column prop="last_login_time" :label="i18n('user.last_login_time')" width="160">
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="290">
            <template #default="scope">
              <el-button v-if="isAuth('kernel:user:form')" type="primary" icon="Edit" @click="router.push({ name: 'kernel_user_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button v-if="isAuth('kernel:user:delete') && 1 != scope.row.id" type="danger" icon="Delete" @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>

              <el-dropdown class="more">
                <el-button icon="More">
                  {{ i18n('common.more') }}
                </el-button>
                <template #dropdown>
                  <el-dropdown-menu>
                    <el-dropdown-item v-if="isAuth('kernel:user:initialization')" @click="initializationHandle(scope.row.id)">
                      {{ i18n('common.initialization_password') }}
                    </el-dropdown-item>
                  </el-dropdown-menu>
                </template>
              </el-dropdown>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next, jumper, total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="user-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const model = ref('user')

  const refresh = inject("refresh")

  const organizationList = ref([])

  const dataForm = reactive([
    'organization_id',
    'nickname',
    'mobile',
    'last_login_time'
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    handleStatus,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    deleteHandle,
  } = useTable(model.value, dataForm)

  const loadOrganizationList = () => {
    $http({
      url: $http.adornUrl('/organization/select'),
      method: 'get',
    }).then(({ data }) => {
      if (data && data.status === 200) {
        organizationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const initializationHandle = (id) => {
    let message = '您确定要初始化当前用户的登录密码？'

    $messagebox.confirm(message, i18n('common.prompt'), {
      confirmButtonText: i18n('common.confirm'),
      cancelButtonText: i18n('common.cancel'),
      type: 'warning'
    }).then(() => {
      $http({
        url: $http.adornUrl('/' + model.value + '/password'),
        method: 'post',
        data: {id: id}
      }).then(({data}) => {
        if (data && data.status === 200) {
          $message.success(i18n('common.handle_success'));
          refresh();
        } else {
          $message.error(data.message)
        }
      })
    }).catch(() => {})
  }

  onMounted(() => {
    getDataList(model)

    loadOrganizationList()
  })
</script>
